# -*- coding:utf-8 -*-
# author: xianting
# time: 2021/5/18 19:06
# file: 01_创建窗口.py

import random
import time

import pygame
from pygame.locals import *


class Base(object):
    def __init__(self, screen_temp, x, y, image_name):
        self.screen = screen_temp
        self.x = x
        self.y = y
        self.image = pygame.image.load(f"./feiji/{image_name}.png")


class BasePlane(Base):
    def __init__(self, screen_temp, x, y, image_name):
        super(BasePlane, self).__init__(screen_temp, x, y, image_name)
        self.bullet_list = []  # 存储发射出去的子弹对象引用

    def display(self):
        self.screen.blit(self.image, (self.x, self.y))
        for bullet in self.bullet_list:
            bullet.display()
            bullet.move()
            yuejie = []
            if bullet.juget():  # 判断子弹是否越界
                yuejie.append(bullet)
            for i in yuejie:
                self.bullet_list.remove(i)


class HeroPlane(BasePlane):
    def __init__(self, screen_temp):
        super(HeroPlane, self).__init__(screen_temp, 210, 700, "hero1")
        self.hit = False
        self.bomb_list = []
        self.__crate_images()
        self.image_num = 0
        self.image_index = 0

    def __crate_images(self):
        self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n1.png"))
        self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n2.png"))
        self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n3.png"))
        self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n4.png"))

    def display(self):
        """显示玩家的飞机"""
        # 如果被击中，就显示爆炸效果，否则显示普通飞机效果
        if self.hit is True:
            self.screen.blit(self.bomb_list[self.image_index], (self.x, self.y))
            self.image_num += 1
            if self.image_num == 7:
                self.image_num = 0
                self.image_index += 1
            if self.image_index > 3:
                time.sleep(1)
                exit()
        else:
            self.screen.blit(self.image, (self.x, self.y))
        # 不管玩家飞机是否被击中，都要显示发射出去的子弹
        for bullet in self.bullet_list:
            bullet.display()
            bullet.move()

    def move_left(self):
        self.x -= 5
        print('left')

    def move_right(self):
        self.x += 5
        print('right')

    def move_up(self):
        self.y -= 5
        print('up')

    def move_down(self):
        self.y += 5
        print('down')

    def fire(self):
        self.bullet_list.append(Bullet(self.screen, self.x, self.y))

    def bomb(self):
        self.hit = True


class EnemyPlane(BasePlane):
    """敌人"""

    def __init__(self, screen_temp):
        super(EnemyPlane, self).__init__(screen_temp, 0, 0, "enemy0")
        self.sudu = random.randint(1, 2)
        self.direction = "right"

    def move(self):
        if self.direction == "right":
            self.x += self.sudu
        elif self.direction == "left":
            self.x -= self.sudu
        if self.x > 430:
            self.direction = "left"
        elif self.x < 0:
            self.direction = "right"

    def fire(self):
        random_num = random.randint(1, 100)
        if random_num == 1 or random_num == 20:
            self.bullet_list.append(EnemyBullet(self.screen, self.x, self.y))


class BaseBullet(Base):
    def __init__(self, screen_temp, x, y, image_name):
        super(BaseBullet, self).__init__(screen_temp, x, y, image_name)

    def display(self):
        self.screen.blit(self.image, (self.x, self.y))


class Bullet(BaseBullet):
    def __init__(self, screen_temp, x, y):
        super(Bullet, self).__init__(screen_temp, x + 40, y - 20, "bullet")

    def move(self):
        self.y -= 2

    def juget(self):
        if self.y < 0:
            return True
        else:
            return False


class EnemyBullet(BaseBullet):
    # 敌人的子弹bullet1
    def __init__(self, screen_temp, x, y):
        super(EnemyBullet, self).__init__(screen_temp, x + 25, y + 40, "bullet1")

    def move(self):
        self.y += 2

    def juget(self):
        if self.y > 852:
            return True
        else:
            return False


def key_control(hero_temp):
    for event in pygame.event.get():
        # 判断是否是点击了退出按钮
        if event.type == QUIT:
            print("exit")
            exit()
        # 判断是否是按下了键
        elif event.type == KEYDOWN:
            # 检测按键是否是a或者left
            if event.key == K_a or event.key == K_LEFT:
                hero_temp.move_left()
            # 检测按键是否是d或者right
            elif event.key == K_d or event.key == K_RIGHT:
                hero_temp.move_right()
            elif event.key == K_w or event.key == K_UP:
                hero_temp.move_up()
            elif event.key == K_s or event.key == K_DOWN:
                hero_temp.move_down()
            # 检测按键是否是空格键
            elif event.key == K_SPACE:
                hero_temp.fire()
                print('space')
            elif event.key == K_b:
                print('b')
                hero_temp.bomb()


def main():
    # 1、创建一个窗口，用来显示内容
    scrren = pygame.display.set_mode((480, 852), 0, 32)
    # 2、创建一个和窗口大小的窗口，用来充当背景
    background = pygame.image.load("./feiji/background.png")

    # 3、创建一个飞机图片
    hero = HeroPlane(scrren)

    # 创建一个敌机
    enemy = EnemyPlane(scrren)

    # 设定需要显示的背景图
    while True:  # 这里的意思是：循环运行整个文件的代码，直到点击关闭按钮，内部应该是设定了延时防止卡死
        scrren.blit(background, (0, 0))  # 该元祖是图片放置的位置，x y
        hero.display()
        # 更新需要显示的内容
        enemy.display()
        enemy.move()  # 调用敌机移动方法
        enemy.fire()
        pygame.display.update()
        # 获取事件，比如按键等
        key_control(hero)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()


if __name__ == '__main__':
    main()
